Skip to content

Additional QuantityArray constructions #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

icweaver
Copy link

@icweaver icweaver commented May 31, 2025

Closes #166

To-do

  • docs
  • method ambiguities

@icweaver icweaver changed the title Additional QuantityArray constructions Additional QuantityArray constructions May 31, 2025
@icweaver icweaver marked this pull request as draft May 31, 2025 00:08
Copy link
Contributor

github-actions bot commented May 31, 2025

Benchmark Results (Julia v1.10)

Time benchmarks
main 12b20d2... main / 12b20d2...
Quantity/creation/Quantity(x) 3.41 ± 0.01 ns 2.79 ± 0.009 ns 1.22 ± 0.0053
Quantity/creation/Quantity(x, length=y) 3.11 ± 0.01 ns 3.41 ± 0.01 ns 0.912 ± 0.004
Quantity/with_numbers/*real 3.1 ± 0.01 ns 3.1 ± 0.01 ns 0.997 ± 0.0045
Quantity/with_numbers/^int 8.05 ± 2.2 ns 8.37 ± 2.5 ns 0.963 ± 0.38
Quantity/with_numbers/^int * real 8.67 ± 2.5 ns 8.05 ± 2.2 ns 1.08 ± 0.42
Quantity/with_quantity/+y 4.04 ± 0.01 ns 4.04 ± 0.001 ns 1 ± 0.0025
Quantity/with_quantity//y 3.42 ± 0.01 ns 3.11 ± 0.001 ns 1.1 ± 0.0032
Quantity/with_self/dimension 3.1 ± 0.01 ns 2.79 ± 0.001 ns 1.11 ± 0.0036
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.001 ns 1 ± 0.0032
Quantity/with_self/ustrip 2.79 ± 0.01 ns 2.79 ± 0.01 ns 1 ± 0.0051
QuantityArray/broadcasting/multi_array_of_quantities 0.147 ± 0.0024 ms 0.144 ± 0.0028 ms 1.02 ± 0.026
QuantityArray/broadcasting/multi_normal_array 0.056 ± 0.0031 ms 0.0529 ± 0.003 ms 1.06 ± 0.084
QuantityArray/broadcasting/multi_quantity_array 0.155 ± 0.00075 ms 0.157 ± 0.00094 ms 0.989 ± 0.0076
QuantityArray/broadcasting/x^2_array_of_quantities 27.5 ± 3.2 μs 26.6 ± 2.7 μs 1.04 ± 0.16
QuantityArray/broadcasting/x^2_normal_array 5.49 ± 0.9 μs 5.68 ± 0.68 μs 0.967 ± 0.2
QuantityArray/broadcasting/x^2_quantity_array 7.14 ± 0.42 μs 7.08 ± 0.41 μs 1.01 ± 0.083
QuantityArray/broadcasting/x^4_array_of_quantities 0.082 ± 0.001 ms 0.0848 ± 0.00066 ms 0.966 ± 0.014
QuantityArray/broadcasting/x^4_normal_array 0.0499 ± 0.00022 ms 0.0471 ± 0.0031 ms 1.06 ± 0.07
QuantityArray/broadcasting/x^4_quantity_array 0.053 ± 0.00031 ms 0.05 ± 0.00023 ms 1.06 ± 0.0079
time_to_load 0.191 ± 0.0036 s 0.19 ± 0.002 s 1 ± 0.022
Memory benchmarks
main 12b20d2... main / 12b20d2...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/multi_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

Copy link
Contributor

github-actions bot commented May 31, 2025

Benchmark Results (Julia v1)

Time benchmarks
main 12b20d2... main / 12b20d2...
Quantity/creation/Quantity(x) 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1 ± 0.0046
Quantity/creation/Quantity(x, length=y) 3.43 ± 0.01 ns 3.73 ± 0.009 ns 0.919 ± 0.0035
Quantity/with_numbers/*real 3.1 ± 0.01 ns 2.79 ± 0.001 ns 1.11 ± 0.0036
Quantity/with_numbers/^int 8.67 ± 1.9 ns 8.67 ± 2.2 ns 1 ± 0.33
Quantity/with_numbers/^int * real 9.29 ± 2.2 ns 9.29 ± 2.2 ns 1 ± 0.33
Quantity/with_quantity/+y 4.35 ± 0.01 ns 4.35 ± 0.009 ns 1 ± 0.0031
Quantity/with_quantity//y 3.11 ± 0.001 ns 3.41 ± 0.01 ns 0.912 ± 0.0027
Quantity/with_self/dimension 3.11 ± 0.01 ns 3.1 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/ustrip 2.79 ± 0.01 ns 3.1 ± 0.01 ns 0.903 ± 0.0044
QuantityArray/broadcasting/multi_array_of_quantities 0.0904 ± 0.00075 ms 0.0904 ± 0.00055 ms 1 ± 0.01
QuantityArray/broadcasting/multi_normal_array 0.0528 ± 0.00026 ms 0.0498 ± 0.00023 ms 1.06 ± 0.0072
QuantityArray/broadcasting/multi_quantity_array 0.0531 ± 0.0092 ms 0.0622 ± 0.00023 ms 0.854 ± 0.15
QuantityArray/broadcasting/x^2_array_of_quantities 13 ± 1.9 μs 13.2 ± 1.2 μs 0.986 ± 0.17
QuantityArray/broadcasting/x^2_normal_array 2.05 ± 2 μs 2.08 ± 1.3 μs 0.986 ± 1.1
QuantityArray/broadcasting/x^2_quantity_array 3.47 ± 0.11 μs 6.47 ± 0.06 μs 0.536 ± 0.018
QuantityArray/broadcasting/x^4_array_of_quantities 0.0842 ± 0.00052 ms 0.0842 ± 0.00072 ms 1 ± 0.011
QuantityArray/broadcasting/x^4_normal_array 0.0497 ± 0.00016 ms 0.0497 ± 0.00014 ms 1 ± 0.0043
QuantityArray/broadcasting/x^4_quantity_array 0.0529 ± 0.00017 ms 0.0468 ± 0.00018 ms 1.13 ± 0.0057
time_to_load 0.198 ± 0.00041 s 0.201 ± 0.0021 s 0.985 ± 0.011
Memory benchmarks
main 12b20d2... main / 12b20d2...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/multi_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
time_to_load 0.159 k allocs: 11.2 kB 0.159 k allocs: 11.2 kB 1

@icweaver icweaver marked this pull request as ready for review June 3, 2025 20:36
@icweaver
Copy link
Author

icweaver commented Jun 3, 2025

Hi @MilesCranmer, this PR should be ready for your review now. Two follow-up questions I had were:

  1. Do we also want to add methods for creating QuantityArrays from generators? I just went with the types defined in ABSTRACT_QUANTITY_TYPES for now
    const ABSTRACT_QUANTITY_TYPES = (
    (AbstractQuantity, Number, Quantity),
    (AbstractGenericQuantity, Any, GenericQuantity),
    (AbstractRealQuantity, Real, RealQuantity)
    )
  2. Did you also want the assorted examples docs for QuantityArray updated, or would you prefer just keeping the explicit QuantityArray(<blah>) constructions there as-is?

@icweaver
Copy link
Author

icweaver commented Jul 10, 2025

Sweet, looks like this is working nicely with the new Makie PR:

julia> using CairoMakie, DynamicQuantities

julia> x = [6, 7, 8]us"cm"
3-element QuantityArray(::Vector{Float64}, ::Quantity{Float64, SymbolicDimensions{FixedRational{Int32, 25200}}}):
 6.0 cm
 7.0 cm
 8.0 cm

julia> y = (4:6)u"kg"
3-element QuantityArray(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
 4.0 kg
 5.0 kg
 6.0 kg

julia> scatter(x, y)
display

@icweaver
Copy link
Author

  1. Did you also want the assorted examples docs for QuantityArray updated, or would you prefer just keeping the explicit QuantityArray() constructions there as-is?

Actually, think I found a good balance, just pushed. Does this look alright to you?

@@ -281,6 +282,8 @@ f_square(v) = v^2 * 1.5 - v^2
println("Applying function to y_q: ", sum(f_square.(y_q)))
```

See [Home > Arrays](@ref home_arrays) for more.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ref work btw?

Copy link
Author

@icweaver icweaver Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just realized I was assuming that index.md was the main source, where this works, not the README.md where it does not. Fixed. 5a59406

@@ -0,0 +1,482 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file got mistakenly committed. It is generated during the Documenter build

Copy link
Author

@icweaver icweaver Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oop! Fixed. f1b4706

Copy link
Member

@MilesCranmer MilesCranmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for this and sorry for the delay. I only had a couple requests

@MilesCranmer
Copy link
Member

Also re: your question, I think creating from generators is probably too much for now.

@icweaver
Copy link
Author

Thanks Miles! Sorry for the mix-up with the README and generated index file; I think things have been straightened out now.

In the process, I think I noticed that the HTML table of contents does not work as intended on my end at least, I think due to the case-sensitivity of the section headers. I'd be happy to open up a separate PR for this and maybe exploring the preprocessing step you mentioned for README code blocks if you think that would be useful

@MilesCranmer
Copy link
Member

Sure! Sounds great, thanks

@@ -281,6 +282,8 @@ f_square(v) = v^2 * 1.5 - v^2
println("Applying function to y_q: ", sum(f_square.(y_q)))
```

See [Home > Arrays](https://ai.damtp.cam.ac.uk/dynamicquantities/stable/#arrays) for more.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an absolute path URL; preferably this should be relative in case I ever change the base URL

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, roger, how's this? 12b20d2

Copy link

codecov bot commented Jul 15, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.62%. Comparing base (b209b7c) to head (12b20d2).

Files with missing lines Patch % Lines
src/math.jl 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #178      +/-   ##
==========================================
- Coverage   99.21%   95.62%   -3.60%     
==========================================
  Files          21       20       -1     
  Lines        1273     1211      -62     
==========================================
- Hits         1263     1158     -105     
- Misses         10       53      +43     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@icweaver
Copy link
Author

icweaver commented Jul 15, 2025

Added a to-do for the method ambiguities picked up by Aqua

Note to self: related reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QuantityArray convenience
2 participants